<template>
  <!-- 检维修记录 -->
  <div class="overhaulRecord">
    <basic-container>
      <avue-crud
        :option="option"
        :table-loading="loading"
        :data="data"
        :page.sync="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crud"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
      >
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {
  getList,
  getDetail,
  add,
  update,
  remove
} from "@/api/basedata/interestedpartymange/relatedpartyqualifications";
import { mapGetters } from "vuex";
import { getToken } from "@/util/auth";

export default {
  props: {
    xgfdwId: {default: ""},
    addRelationDialog: {default: false}
  },
  data() {
    return {
      form: {},
      query: {},
      data: [],
      loading: true,
      selectionList: [],
      page: {
        pageSize: this.$pageSize,
        currentPage: 1,
        total: 0
      },
      option: {
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchSpan: 6,
        searchMenuSpan: 6,
        border: true,
        index: false,
        selection: true,
        menu: false,
        viewBtn: false,
        editBtn: false,
        addBtn: false,
        delBtn: false,
        refreshBtn: false,
        columnBtn: false,
        dialogClickModal: false,
        menuWidth: 100,
        column: [
          {
            label: "设备名称",
            prop: "zzzsbh",
            align: "center",
          },
          {
            label: "设备类别",
            prop: "dec",
            align: "center",
          },
          {
            label: "检修类别",
            prop: "name",
            align: "center",
          }
          ,
          {
            label: "开工时间",
            prop: "per",
            align: "center",
          }
          ,
          {
            label: "结束时间",
            prop: "date",
            align: "center",
          }
          ,
          {
            label: "操作人",
            prop: "PDA",
            align: "center",
          }
          ,
          {
            label: "交付人",
            prop: "next",
            align: "center",
          }
          ,
          {
            label: "接收人",
            prop: "result",
            align: "center",
          }
          
        ]
      },
      // 附件
      dialogType: "",
      downloadHead: "/api/fileManager/download/",
      fjFileList: [],
      SetHeaders: { "Blade-Auth": getToken() },
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    // 按钮权限
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.menu_add, false),
          viewBtn: this.vaildData(this.permission.menu_view, false),
          delBtn: this.vaildData(this.permission.menu_delete, false),
          editBtn: this.vaildData(this.permission.menu_edit, false)
        };
      },
    // 勾选项ID
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  watch: {
    addRelationDialog() {
      if(this.addRelationDialog === true) {
        this.initData();
      }
    },
  },
  mounted() {
    this.initData();
  },
  methods: {

    // 初始化数据
    initData() {
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },

    // 新增
    rowSave(row, done, loading) {
      row.xgfdw = this.xgfdwId;
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          window.console.log(error);
        }
      );
    },
    // 修改
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        },
        error => {
          loading();
          console.log(error);
        }
      );
    },

    // 删除一条
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },

    // 删除多条
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },

    // 打开新增、编辑、查看弹窗前
    beforeOpen(done, type) {
      this.dialogType = type;
      this.fjFileList = [];
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
          if (this.form.fj && this.form.fjFile && JSON.stringify(this.form.fjFile) !== "{}") {
            this.fjFileList.push({
              name: this.form.fjFile.fileName,
              response: {
                code: 200,
                msg: "操作成功",
                success: true,
                data: this.form.fjFile
              }
            });
          }
        });
      }
      done();
    },

    // 清空搜索条件
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },

    // 搜索条件改变
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },

    // 勾选
    selectionChange(list) {
      this.selectionList = list;
    },

    // 清空勾选
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },

    // 更换页数
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },

    // 更换每页显示数量
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },

    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    },

    // 加载数据
    onLoad(page, params = {}) {
      this.loading = true;
      this.query["xgfdw"] = this.xgfdwId;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    },

    /**
     * 上传附件
     */

    // 上传附件前
    beforeUploadFJ(file) {
      console.log(file);
      var isLimit = this.fjFileList.length + 1 <= 1;
      if (!isLimit) {
        this.$message.warning("只能选择一个附件!");
      }
      return isLimit;
    },

    // 上传附件成功
    uploadFJSuccess(response, file, fileList) {
      if (response.code !== 200 || response.success !== true) {
        return;
      }
      this.fjFileList = fileList;
      this.form.fj = response.data.id;
    },

    // 删除附件
    handleRemoveFJ(file, fileList) {
      this.fjFileList = fileList;
      this.form.fj = "";
    },

  }
};
</script>

<style lang="scss" scpoed>
.overhaulRecord {
  .enclosure_div {
    a {
      font-size: 12px;
      color: #409eff;

      i {
        margin-right: 5px;
      }
    }
  }
}
</style>